package controller

import (
	"github.com/gin-gonic/gin"
	"hanyun-admin/src/entity"
	"hanyun-admin/src/service"
	"net/http"
	"strconv"
	"time"
)
//获取字典类型
func GetDictTypeList(c *gin.Context)  {
	var dictType entity.DictType
	c.Bind(&dictType)
	dictTypeList := service.SelectDictTypeList(dictType)
	count := service.SelectDictTypeListCount(dictType)
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":0,
		"rows":dictTypeList,
		"total":count,
	})
}

//根据字典类型查询字典数据信息
func GetDictDataListByDictType(c *gin.Context)  {
	dictData := service.SelectDictDataByType(c.Param("dictType"))
	c.JSON(200,gin.H{
		"code":200,
		"data":dictData,
		"msg":"操作成功",
	})
}

//获取字典数据集
func GetDictDataList(c *gin.Context)  {
	var dictData entity.DictData
	c.Bind(&dictData)
	dictDataList := service.SelectDictDataList(dictData)
	dictDataListCount := service.SelectDictDataListCount(dictData)
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":0,
		"rows":dictDataList,
		"total":dictDataListCount,
	})
}
//获取字典类型详情
func GetDictTypeInfo(c *gin.Context)  {
	dictId,_ := strconv.ParseInt(c.Param("dictId"),10,64)
	dictType := service.SelectDictTypeByDictId(dictId)
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"data":dictType,
		"msg":"操作成功",
	})
}
//保存字典类型
func AddSysDictType(c *gin.Context)  {
	var dictType entity.DictType
	c.BindJSON(&dictType)
	dictType.CreateTime = time.Now()
	err := service.InsertDictType(dictType)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code":http.StatusInternalServerError,
			"msg":"操作失败",
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":"操作成功",
	})
}
//更新数据
func UpdateSysDictType(c *gin.Context)  {
	var dictType entity.DictType
	c.BindJSON(&dictType)
	err := service.UpdateDictType(dictType)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code":http.StatusInternalServerError,
			"msg":"操作失败",
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":"操作成功",
	})
}
//删除数据
func DeleteSysDictType(c *gin.Context)  {
	dictId,_ := strconv.ParseInt(c.Param("dictId"),10,64)
	err := service.DeleteDictType(dictId)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code":http.StatusInternalServerError,
			"msg":"操作失败",
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":"操作成功",
	})
}
//新增字典数据
func AddSysDictData(c *gin.Context)  {
	var dictData entity.DictData
	c.BindJSON(&dictData)
	dictData.CreateTime = time.Now()
	err := service.InsertDictDate(dictData)
	if err!=nil {
		c.JSON(http.StatusOK,gin.H{
			"code":http.StatusInternalServerError,
			"msg":"操作失败",
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":"操作成功",
	})
}
//查询字典数据详情
func GetDictDataInfo(c *gin.Context)  {
	dictCode,_ := strconv.ParseInt(c.Param("dictCode"),10,64)
	dictData := service.SelectDictDataByDictCode(dictCode)
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"data":dictData,
		"msg":"操作成功",
	})
}
//更新字典数据
func UpdateSysDictData(c *gin.Context)  {
	var dictData entity.DictData
	c.BindJSON(&dictData)
	err := service.UpdateDictData(dictData)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code":http.StatusInternalServerError,
			"msg":"操作失败",
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":"操作成功",
	})
}
//删除字典数据
func DeleteSysDictData(c *gin.Context)  {
	dictCode, _ := strconv.ParseInt(c.Param("dictCode"),10,64)
	err := service.DeleteDictData(dictCode)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code":http.StatusInternalServerError,
			"msg":"操作失败",
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code":http.StatusOK,
		"msg":"操作成功",
	})
}